package ces.lawsuit.controller;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import ces.lawsuit.model.Menu;
import ces.lawsuit.util.AppUtil;
import ces.lawsuit.validator.MenuValidator;

public class MenuController extends BaseController<Menu> {

  /**
   * 初始化菜单
   * 
   **/
  private void init(){
      setAttr("menu",  Menu.me.findById(getParaToLong("id")));
	  setAttr("menuList",Menu.me.find(Menu.LIST_SQL));
  }
  /***
   *列表页面
   */
   public void index() {
		setAttr("menuList", Menu.me.find(Menu.LIST_SQL));
		render("/admin/menu/list.html");
	}
   /**
    * 新增或者保存
    * **/
    @Before(MenuValidator.class)
	public void saveOrUpdate(){
		Menu menu=getModel(Menu.class);
		if(StrKit.isBlank(menu.getStr("id"))){
			menu.set("id", AppUtil.generateId());
			menu.save();
		}else{
			menu.update();
		}
		renderSuccess();
	}
	
	public void show(){
		init();
		render("/admin/menu/edit.html");
	}
	/**
	 *删除 
	 **/
	public void delete() {
		String ids=getPara(0);
	    if(StrKit.notBlank(ids)){
	    	String[] attrs=ids.split(",");
	    	for(String id:attrs){
	    		Menu.me.deleteById(id);
	    	}
	    }
		renderSuccess();
	}
}
